clear 
clear matrix
set memory 1000m
set more off
cap log close

cd "/Users/..."

global do_file="‎⁨/Users/.../do_file"
global log_file="/Users/.../log_file"
global raw_data="/Users/.../raw_data⁩⁩"
global working_data="/Users/.../working_data"
global results="/Users/.../results"
global results="/Users/.../results/072020/main_reg"


log using log_file/reg_day_072020.log, replace


*****************
*****************
* Use Full Sample
*****************
*****************



**********************************
* Main Results: Baseline + IV Spec
**********************************


use working_data/clean_day_level_geo_weather_081519.dta, clear

sort user_id trip_start_date
bysort user_id: gen day_index=_n

tsset user_id day_index

** Create measures for NM and generate differenced variables
do do_file/dofile_072020/gen_nm_diff.do

** summarize average levels of the risky behavior
sum total_phone_use distance duration speed drive_at_night highway


estpost tabstat total_phone_use distance duration speed drive_at_night highway, listwise statistics(mean)
esttab . using $results/avg_values.csv, replace cells("total_phone_use distance duration speed drive_at_night highway") noobs




** Start  Regressions

* just use this line to start the table
ivregress gmm diff_agg_acc l.diff_hb1_dummy diff_traffic_jam diff_weekend diff_rush_hour ///
diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
(l.diff_agg_acc = l(2).total_agg_acc), vce(cluster user_id) nocons
outreg2 using $results/reg_full_072020.xls, replace keep(l.diff_hb1_dummy) 

** baseline 
foreach nm of varlist diff_hb1_dummy-diff_hb6_dummy{

	

	ivregress gmm diff_phone_use l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_phone_use = l(2).total_phone_use), vce(cluster user_id) nocons
	outreg2 using $results/reg_full_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_distance = l(2).distance), vce(cluster user_id) nocons
	outreg2 using $results/reg_full_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_duration = l(2).duration), vce(cluster user_id) nocons
	outreg2 using $results/reg_full_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_speed = l(2).speed), vce(cluster user_id) nocons
	outreg2 using $results/reg_full_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_drive_at_night = l(2).drive_at_night), vce(cluster user_id) nocons
	outreg2 using $results/reg_full_072020.xls, append keep(l.`nm')


	ivregress gmm diff_highway l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_highway = l(2).highway), vce(cluster user_id) nocons
	outreg2 using $results/reg_full_072020.xls, append keep(l.`nm')
	
	
}


** IV specification
foreach nm of varlist diff_hb1_dummy-diff_hb6_dummy{

	

	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_full_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_full_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_full_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_full_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_full_072020.xls, append keep(l.`nm')
	
	
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_full_072020.xls, append keep(l.`nm')
	
	
	
}



** IV specification, add an additional lag, only use six nm measures

* just use this line to start the table
ivregress gmm diff_agg_acc l(1/2).diff_hb1_dummy diff_traffic_jam diff_weekend diff_rush_hour ///
diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
(l.diff_agg_acc = l(2).total_agg_acc), vce(cluster user_id) nocons
outreg2 using $results/reg_add_2lags_072020.xls, replace keep(l.diff_hb1_dummy) 

foreach nm of varlist diff_hb1_dummy-diff_hb9{

	
	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour l(2).diff_phone_use l(2).`nm' ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow l(2).diff_drive_score l(1).diff_gap_time ///
	(l.diff_phone_use l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow ), vce(cluster user_id) nocons
	outreg2 using $results/reg_add_2lags_072020.xls, append keep(l(1/2).`nm')
	
	
	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour l(2).diff_distance l(2).`nm' ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow l(2).diff_drive_score l(1).diff_gap_time ///
	(l.diff_distance l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow ), vce(cluster user_id) nocons
	outreg2 using $results/reg_add_2lags_072020.xls, append keep(l(1/2).`nm')
	
	
	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour l(2).diff_duration l(2).`nm' ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow l(2).diff_drive_score l(1).diff_gap_time ///
	(l.diff_duration l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow ), vce(cluster user_id) nocons
	outreg2 using $results/reg_add_2lags_072020.xls, append keep(l(1/2).`nm')	
	
	
	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour l(2).diff_speed l(2).`nm' ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow l(2).diff_drive_score l(1).diff_gap_time ///
	(l.diff_speed l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow ), vce(cluster user_id) nocons
	outreg2 using $results/reg_add_2lags_072020.xls, append keep(l(1/2).`nm')
	
	
	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour l(2).diff_drive_at_night l(2).`nm' ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow l(2).diff_drive_score l(1).diff_gap_time ///
	(l.diff_drive_at_night l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow ), vce(cluster user_id) nocons
	outreg2 using $results/reg_add_2lags_072020.xls, append keep(l(1/2).`nm')
	
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour l(2).diff_highway l(2).`nm' ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow l(2).diff_drive_score l(1).diff_gap_time ///
	(l.diff_highway l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow ), vce(cluster user_id) nocons
	outreg2 using $results/reg_add_2lags_072020.xls, append keep(l(1/2).`nm')
	
}




**********************************
* Robusness Check: regular drivers
**********************************

* one concern: after bad weather, drivers may only take short, low stress trip
* so weather(t-2) may be correlated with epsilon(t-1)
* for regular drivers, take almost same trip every day, won't adjust trips


use working_data/clean_day_level_geo_weather_081519.dta, clear

sort user_id trip_start_date
bysort user_id: gen day_index=_n
bysort user_id: gen total_days=_N

tsset user_id day_index

** Create measures for NM and generate differenced variables
do do_file/dofile_072020/gen_nm_diff.do


gen trip_start_date_new=date(trip_start_date,"YMD")
format trip_start_date_new %td

gen temp=trip_start_date_new if day_index==1
bysort user_id: egen first_drive_date=total(temp)
drop temp

gen temp=trip_start_date_new if day_index==total_days
bysort user_id: egen last_drive_date=total(temp)
drop temp

gen active_days=last_drive_date-first_drive_date+1
gen active_ratio=total_days/active_days

sum active_ratio if day_index==1, d
count if active_ratio==1 & day_index==1
sum total_days if active_ratio==1 & day_index==1, d

bysort user_id day_of_week: egen distance_mean=mean(distance)
forvalues i=1(1)7{

	gen temp=distance_mean if day_of_week==`i'-1
	bysort user_id: egen distance_mean_`i'=min(temp)
	drop temp

}


egen weekday_distance_mean=rowmean(distance_mean_2-distance_mean_6)
// missing value means: do not drive on weekdays

egen weekday_distance_std=rowsd(distance_mean_2-distance_mean_6)
// missing value means: do not drive on weekdays or only drive on one weekday

gen weekday_distance_ratio=weekday_distance_std/weekday_distance_mean

egen weekday_drive_nomiss=rownonmiss(distance_mean_2-distance_mean_6)


sum weekday_distance_ratio if day_index==1 & weekday_drive_nomiss==5, d
sum weekday_distance_ratio if active_ratio==1 & day_index==1 & weekday_drive_nomiss==5, d


sort user_id day_index
tsset user_id day_index


keep if weekday_distance_ratio<=.2369957 & weekday_drive_nomiss==5
// use 25 percentile as a threshold

** summarize average levels of the risky behavior
sum total_phone_use distance duration speed drive_at_night highway
estpost tabstat total_phone_use distance duration speed drive_at_night highway, listwise statistics(mean)
esttab . using $results/avg_values.csv, append cells("total_phone_use distance duration speed drive_at_night highway") noobs



** Start  Regressions

* just use this line to start the table
ivregress gmm diff_agg_acc l.diff_hb1_dummy diff_traffic_jam diff_weekend diff_rush_hour ///
diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
(l.diff_agg_acc = l(2).total_agg_acc), vce(cluster user_id) nocons
outreg2 using $results/reg_regular_072020.xls, replace keep(l.diff_hb1_dummy) 

** baseline 

foreach nm of varlist diff_hb1_dummy-diff_hb9{
	

	ivregress gmm diff_phone_use l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_phone_use = l(2).total_phone_use), vce(cluster user_id) nocons
	outreg2 using $results/reg_regular_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_distance = l(2).distance), vce(cluster user_id) nocons
	outreg2 using $results/reg_regular_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_duration = l(2).duration), vce(cluster user_id) nocons
	outreg2 using $results/reg_regular_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_speed = l(2).speed), vce(cluster user_id) nocons
	outreg2 using $results/reg_regular_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_drive_at_night = l(2).drive_at_night), vce(cluster user_id) nocons
	outreg2 using $results/reg_regular_072020.xls, append keep(l.`nm')


	ivregress gmm diff_highway l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_highway = l(2).highway), vce(cluster user_id) nocons
	outreg2 using $results/reg_regular_072020.xls, append keep(l.`nm')
	
	
}

** IV specification
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_regular_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_regular_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_regular_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_regular_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_regular_072020.xls, append keep(l.`nm')
		
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_regular_072020.xls, append keep(l.`nm')
		
}


***********************************************
* Robusness Check: early rounds vs later rounds
***********************************************

use working_data/clean_day_level_geo_weather_081519.dta, clear

sort user_id trip_start_date
bysort user_id: gen day_index=_n
bysort user_id: gen total_days=_N


tsset user_id day_index

** Create measures for NM and generate differenced variables
do do_file/dofile_072020/gen_nm_diff.do




* for later rounds, learning story is less concerned

sum total_days if day_index==1, d
hist total_days if day_index==1


* first try using 30 days as a cutoff (saved in later_early_30days)
gen later_days1=1 if day_index>=30 
replace later_days1=0 if later_days1==. & total_days>=30

* then try using 60 days as a cutoff (saved in later_early_60days)
gen later_days2=1 if day_index>=60
replace later_days2=0 if later_days2==. & total_days>=60


gen early_days1=1 if day_index<30
gen early_days2=1 if day_index<60



tab later_days1 
tab later_days2


sum total_phone_use distance duration speed drive_at_night highway if early_days1==1
sum total_phone_use distance duration speed drive_at_night highway if later_days1==1
sum total_phone_use distance duration speed drive_at_night highway if early_days2==1
sum total_phone_use distance duration speed drive_at_night highway if later_days2==1


estpost tabstat total_phone_use distance duration speed drive_at_night highway if later_days2==1, listwise statistics(mean)
esttab . using $results/avg_values.csv, append cells("total_phone_use distance duration speed drive_at_night highway") noobs

estpost tabstat total_phone_use distance duration speed drive_at_night highway if early_days2==1, listwise statistics(mean)
esttab . using $results/avg_values.csv, append cells("total_phone_use distance duration speed drive_at_night highway") noobs


sort user_id day_index
tsset user_id day_index


* Start  Regressions for later days: day_index>=60

* just use this line to start the table
ivregress gmm diff_agg_acc l.diff_hb1_dummy diff_traffic_jam diff_weekend diff_rush_hour ///
diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
(l.diff_agg_acc = l(2).total_agg_acc) if later_days2==1, vce(cluster user_id) nocons
outreg2 using $results/reg_later60_072020.xls, replace keep(l.diff_hb1_dummy) 

** baseline 
foreach nm of varlist diff_hb1_dummy-diff_hb9{
	

	ivregress gmm diff_phone_use l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_phone_use = l(2).total_phone_use) if later_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_later60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_distance = l(2).distance) if later_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_later60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_duration = l(2).duration) if later_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_later60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_speed = l(2).speed) if later_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_later60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_drive_at_night = l(2).drive_at_night) if later_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_later60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_highway l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_highway = l(2).highway) if later_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_later60_072020.xls, append keep(l.`nm')
	
	
}

** IV specification
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow) if later_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_later60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow) if later_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_later60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow) if later_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_later60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow) if later_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_later60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow) if later_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_later60_072020.xls, append keep(l.`nm')
		
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow) if later_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_later60_072020.xls, append keep(l.`nm')
		
}




** Start  Regressions for earlier days: day_index<60

* just use this line to start the table
ivregress gmm diff_agg_acc l.diff_hb1_dummy diff_traffic_jam diff_weekend diff_rush_hour ///
diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
(l.diff_agg_acc = l(2).total_agg_acc) if early_days2==1, vce(cluster user_id) nocons
outreg2 using $results/reg_early60_072020.xls, replace keep(l.diff_hb1_dummy) 


** baseline 
foreach nm of varlist diff_hb1_dummy-diff_hb9{
	

	ivregress gmm diff_phone_use l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_phone_use = l(2).total_phone_use) if early_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_early60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_distance = l(2).distance) if early_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_early60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_duration = l(2).duration) if early_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_early60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_speed = l(2).speed) if early_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_early60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_drive_at_night = l(2).drive_at_night) if early_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_early60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_highway l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_highway = l(2).highway) if early_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_early60_072020.xls, append keep(l.`nm')
	
	
}



** IV specification
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow) if early_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_early60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow) if early_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_early60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow) if early_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_early60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow) if early_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_early60_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow) if early_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_early60_072020.xls, append keep(l.`nm')
		
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow) if early_days2==1, vce(cluster user_id) nocons
	outreg2 using $results/reg_early60_072020.xls, append keep(l.`nm')
		
}



**********************************************
**********************************************
* Use Small Sample: Users who requested quotes
**********************************************
**********************************************


use working_data/merge_quote_drive_081619.dta, clear



gen temp=date(vehicle_register_date, "YMD")
bysort user_id: egen car_regis_date=min(temp) 
drop temp

gen temp=substr(vehicle_register_date, 1, 4)
destring temp, replace force
bysort user_id: egen car_regis_year=min(temp)
drop temp

count if car_regis_date==.
tab car_regis_year

replace car_regis_year=. if car_regis_year==1970


gen temp=substr(user_id_num, 7, 4)
destring temp, replace force
bysort user_id: egen user_birth_year=min(temp)
drop temp

tab user_birth_year
replace user_birth_year=. if user_birth_year<1951

tab user_id_num if user_birth_year==8632, m
replace user_birth_year=. if user_birth_year==8632

tab user_birth_year



drop if trip_start_date==""

sort user_id trip_start_date
bysort user_id: gen day_index=_n

tsset user_id day_index



tab car_regis_year
tab user_birth_year



** Create measures for NM and generate differenced variables
do do_file/dofile_072020/gen_nm_diff.do

** summarize average levels of the risky behavior
sum total_phone_use distance duration speed drive_at_night highway if car_regis_year<2016
sum total_phone_use distance duration speed drive_at_night highway if car_regis_year>=2016
sum total_phone_use distance duration speed drive_at_night highway if user_birth_year<1985


estpost tabstat total_phone_use distance duration speed drive_at_night highway if car_regis_year<2016, listwise statistics(mean)
esttab . using $results/avg_values.csv, append cells("total_phone_use distance duration speed drive_at_night highway") noobs
 
estpost tabstat total_phone_use distance duration speed drive_at_night highway if car_regis_year>=2016, listwise statistics(mean)
esttab . using $results/avg_values.csv, append cells("total_phone_use distance duration speed drive_at_night highway") noobs

estpost tabstat total_phone_use distance duration speed drive_at_night highway if user_birth_year<1985, listwise statistics(mean)
esttab . using $results/avg_values.csv, append cells("total_phone_use distance duration speed drive_at_night highway") noobs
 
********************************************
* Robustness Check: more experienced drivers 
********************************************
* use car registration year 

preserve

keep if car_regis_year<2016


* just use this line to start the table
ivregress gmm diff_agg_acc l.diff_hb1_dummy diff_traffic_jam diff_weekend diff_rush_hour ///
diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
(l.diff_agg_acc = l(2).total_agg_acc), vce(cluster user_id) nocons
outreg2 using $results/reg_exp_072020.xls, replace keep(l.diff_hb1_dummy) 


** baseline 
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_phone_use = l(2).total_phone_use), vce(cluster user_id) nocons
	outreg2 using $results/reg_exp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_distance = l(2).distance), vce(cluster user_id) nocons
	outreg2 using $results/reg_exp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_duration = l(2).duration), vce(cluster user_id) nocons
	outreg2 using $results/reg_exp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_speed = l(2).speed), vce(cluster user_id) nocons
	outreg2 using $results/reg_exp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_drive_at_night = l(2).drive_at_night), vce(cluster user_id) nocons
	outreg2 using $results/reg_exp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_highway l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_highway = l(2).highway), vce(cluster user_id) nocons
	outreg2 using $results/reg_exp_072020.xls, append keep(l.`nm')
	
	
}


** IV specification
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_exp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_exp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_exp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_exp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_exp_072020.xls, append keep(l.`nm')
	
	
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_exp_072020.xls, append keep(l.`nm')
	
	
	
}



restore


********************************************
* Robustness Check: less experienced drivers 
********************************************
* use car registration year

preserve

keep if car_regis_year>=2016 & car_regis_year~=.



* just use this line to start the table
ivregress gmm diff_agg_acc l.diff_hb1_dummy diff_traffic_jam diff_weekend diff_rush_hour ///
diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
(l.diff_agg_acc = l(2).total_agg_acc), vce(cluster user_id) nocons
outreg2 using $results/reg_inexp_072020.xls, replace keep(l.diff_hb1_dummy) 

** baseline 
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_phone_use = l(2).total_phone_use), vce(cluster user_id) nocons
	outreg2 using $results/reg_inexp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_distance = l(2).distance), vce(cluster user_id) nocons
	outreg2 using $results/reg_inexp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_duration = l(2).duration), vce(cluster user_id) nocons
	outreg2 using $results/reg_inexp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_speed = l(2).speed), vce(cluster user_id) nocons
	outreg2 using $results/reg_inexp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_drive_at_night = l(2).drive_at_night), vce(cluster user_id) nocons
	outreg2 using $results/reg_inexp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_highway l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_highway = l(2).highway), vce(cluster user_id) nocons
	outreg2 using $results/reg_inexp_072020.xls, append keep(l.`nm')
	
	
}

** IV specification
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_inexp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_inexp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_inexp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_inexp_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_inexp_072020.xls, append keep(l.`nm')
	
	
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_inexp_072020.xls, append keep(l.`nm')
	
	
	
}


restore


********************************************
* Robustness Check: more experienced drivers 
********************************************
* use user birth year

preserve

keep if user_birth_year<1985


bysort user_id: gen temp=_n
count if temp==1 // only has 128 users in this sample




* just use this line to start the table
ivregress gmm diff_agg_acc l.diff_hb1_dummy diff_traffic_jam diff_weekend diff_rush_hour ///
diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
(l.diff_agg_acc = l(2).total_agg_acc), vce(cluster user_id) nocons
outreg2 using $results/reg_old_072020.xls, replace keep(l.diff_hb1_dummy) 

** baseline 
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_phone_use = l(2).total_phone_use), vce(cluster user_id) nocons
	outreg2 using $results/reg_old_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_distance = l(2).distance), vce(cluster user_id) nocons
	outreg2 using $results/reg_old_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_duration = l(2).duration), vce(cluster user_id) nocons
	outreg2 using $results/reg_old_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_speed = l(2).speed), vce(cluster user_id) nocons
	outreg2 using $results/reg_old_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_drive_at_night = l(2).drive_at_night), vce(cluster user_id) nocons
	outreg2 using $results/reg_old_072020.xls, append keep(l.`nm')


	ivregress gmm diff_highway l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_highway = l(2).highway), vce(cluster user_id) nocons
	outreg2 using $results/reg_old_072020.xls, append keep(l.`nm')
	
	
}

** IV specification
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_old_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_old_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_old_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_old_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_old_072020.xls, append keep(l.`nm')
	
	
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_old_072020.xls, append keep(l.`nm')
	
	
	
}



restore




****************************************
* Robustness Check: NM on familiar roads
****************************************

use working_data/clean_day_level_geo_weather_familiar_012020.dta, clear

sort user_id trip_start_date
bysort user_id: gen day_index=_n

tsset user_id day_index

sum total_phone_use distance duration speed drive_at_night highway 
sum total_phone_use distance duration speed drive_at_night highway if familiar_trip_dummy==1
sum total_phone_use distance duration speed drive_at_night highway if familiar_trip_dummy==0



************************************
* Construct measures for near-misses
************************************


gen hb1=total_prev_hb_familiar
gen hb2=total_real_hb1_familiar
gen hb3=total_hard_hb_familiar

gen hb4=hb1+total_hard_turn_familiar
gen hb5=hb2+total_hard_turn_familiar
gen hb6=hb3+total_hard_turn_familiar


forvalues i=1(1)6{
	gen hb`i'_dummy=1 if hb`i'>0
	replace hb`i'_dummy=0 if hb`i'_dummy==.
}


forvalues i=7(1)9{
	loc j=`i'-6
	display `j'

	gen hb`i'=1 if hb`j'>0 & total_hard_turn_familiar>0
	replace hb`i'=0 if hb`i'==.
}


sum hb1-hb9

***********************
* Gen fisrt-diff for NM
***********************


forvalues i=1(1)6{
	gen diff_hb`i'=hb`i'-l.hb`i'
}

forvalues i=1(1)6{
	gen diff_hb`i'_dummy=hb`i'_dummy-l.hb`i'_dummy
}

forvalues i=7(1)9{
	gen diff_hb`i'=hb`i'-l.hb`i'
}


order diff_hb1_dummy-diff_hb3_dummy diff_hb7-diff_hb9, after(hb9)
// diff_hb1_dummy-diff_hb9, reported in the main tables
// diff_hb1-diff_hb6_dummy, reporeted in the robustness checks

**********************************************
* Gen fisrt-diff for risky behavior and others
**********************************************


gen diff_agg_acc=total_agg_acc-l.total_agg_acc

gen diff_phone_use=total_phone_use-l.total_phone_use

gen diff_traffic_jam=total_traffic_jam-l.total_traffic_jam
gen diff_traffic_time=total_traffic_sec-l.total_traffic_sec
gen diff_distance=distance-l.distance
gen diff_duration=duration-l.duration
gen diff_speed=speed-l.speed
gen diff_speed2=speed2-l.speed2
gen diff_drive_at_night=drive_at_night-l.drive_at_night
gen diff_night_prop=night_duration_prop-l.night_duration_prop
gen diff_night_duration=total_night_duration-l.total_night_duration
gen diff_weekend=weekend-l.weekend
gen diff_rush_hour=rush_hour-l.rush_hour

gen diff_control_score=control_score-l.control_score
gen diff_cautious_score=cautious_score-l.cautious_score
gen diff_focused_score=focused_score-l.focused_score
gen diff_drive_score=drive_score-l.drive_score
gen diff_gap_time=gap_time-l.gap_time

gen diff_highway=highway-l.highway

** gen first_difference for weather variables, 
gen diff_sunny=sunny-l.sunny
gen diff_rain=rain-l.rain
gen diff_snow=snow-l.snow
gen diff_rain_storm=rain_storm-l.rain_storm
gen diff_cloudy=cloudy-l.cloudy

gen diff_high_temper=high_temper-l.high_temper
gen diff_low_temper=low_temper-l.low_temper

sum total_phone_use distance duration speed drive_at_night highway


** Start  Regressions for familiar roads


* just use this line to start the table
ivregress gmm diff_agg_acc l.diff_hb1_dummy diff_traffic_jam diff_weekend diff_rush_hour ///
diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
(l.diff_agg_acc = l(2).total_agg_acc) , vce(cluster user_id) nocons
outreg2 using $results/reg_familiarNM_072020_new.xls, replace keep(l.diff_hb1_dummy) 


** baseline 
foreach nm of varlist diff_hb1_dummy-diff_hb9{
	

	ivregress gmm diff_phone_use l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_phone_use = l(2).total_phone_use) , vce(cluster user_id) nocons
	outreg2 using $results/reg_familiarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_distance l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_distance = l(2).distance) , vce(cluster user_id) nocons
	outreg2 using $results/reg_familiarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_duration l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_duration = l(2).duration) , vce(cluster user_id) nocons
	outreg2 using $results/reg_familiarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_speed l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_speed = l(2).speed) , vce(cluster user_id) nocons
	outreg2 using $results/reg_familiarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_drive_at_night = l(2).drive_at_night) , vce(cluster user_id) nocons
	outreg2 using $results/reg_familiarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_highway l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_highway = l(2).highway) , vce(cluster user_id) nocons
	outreg2 using $results/reg_familiarNM_072020_new.xls, append keep(l.`nm')
	
	
}



** IV specification
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow)  , vce(cluster user_id) nocons
	outreg2 using $results/reg_familiarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow)  , vce(cluster user_id) nocons
	outreg2 using $results/reg_familiarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow)  , vce(cluster user_id) nocons
	outreg2 using $results/reg_familiarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow)  , vce(cluster user_id) nocons
	outreg2 using $results/reg_familiarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow)  , vce(cluster user_id) nocons
	outreg2 using $results/reg_familiarNM_072020_new.xls, append keep(l.`nm')
		
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow)  , vce(cluster user_id) nocons
	outreg2 using $results/reg_familiarNM_072020_new.xls, append keep(l.`nm')
		
}



********************************************
* Robustness Check: NM on non-familiar roads
********************************************




use working_data/clean_day_level_geo_weather_familiar_012020.dta, clear

sort user_id trip_start_date
bysort user_id: gen day_index=_n

tsset user_id day_index




************************************
* Construct measures for near-misses
************************************


gen hb1=total_prev_hb_nonfamiliar
gen hb2=total_real_hb1_nonfamiliar
gen hb3=total_hard_hb_nonfamiliar

gen hb4=hb1+total_hard_turn_nonfamiliar
gen hb5=hb2+total_hard_turn_nonfamiliar
gen hb6=hb3+total_hard_turn_nonfamiliar


forvalues i=1(1)6{
	gen hb`i'_dummy=1 if hb`i'>0
	replace hb`i'_dummy=0 if hb`i'_dummy==.
}


forvalues i=7(1)9{
	loc j=`i'-6
	display `j'

	gen hb`i'=1 if hb`j'>0 & total_hard_turn_nonfamiliar>0
	replace hb`i'=0 if hb`i'==.
}


sum hb1-hb9

***********************
* Gen fisrt-diff for NM
***********************


forvalues i=1(1)6{
	gen diff_hb`i'=hb`i'-l.hb`i'
}

forvalues i=1(1)6{
	gen diff_hb`i'_dummy=hb`i'_dummy-l.hb`i'_dummy
}

forvalues i=7(1)9{
	gen diff_hb`i'=hb`i'-l.hb`i'
}


order diff_hb1_dummy-diff_hb3_dummy diff_hb7-diff_hb9, after(hb9)
// diff_hb1_dummy-diff_hb9, reported in the main tables
// diff_hb1-diff_hb6_dummy, reporeted in the robustness checks

**********************************************
* Gen fisrt-diff for risky behavior and others
**********************************************


gen diff_agg_acc=total_agg_acc-l.total_agg_acc

gen diff_phone_use=total_phone_use-l.total_phone_use

gen diff_traffic_jam=total_traffic_jam-l.total_traffic_jam
gen diff_traffic_time=total_traffic_sec-l.total_traffic_sec
gen diff_distance=distance-l.distance
gen diff_duration=duration-l.duration
gen diff_speed=speed-l.speed
gen diff_speed2=speed2-l.speed2
gen diff_drive_at_night=drive_at_night-l.drive_at_night
gen diff_night_prop=night_duration_prop-l.night_duration_prop
gen diff_night_duration=total_night_duration-l.total_night_duration
gen diff_weekend=weekend-l.weekend
gen diff_rush_hour=rush_hour-l.rush_hour

gen diff_control_score=control_score-l.control_score
gen diff_cautious_score=cautious_score-l.cautious_score
gen diff_focused_score=focused_score-l.focused_score
gen diff_drive_score=drive_score-l.drive_score
gen diff_gap_time=gap_time-l.gap_time

gen diff_highway=highway-l.highway

** gen first_difference for weather variables, 
gen diff_sunny=sunny-l.sunny
gen diff_rain=rain-l.rain
gen diff_snow=snow-l.snow
gen diff_rain_storm=rain_storm-l.rain_storm
gen diff_cloudy=cloudy-l.cloudy

gen diff_high_temper=high_temper-l.high_temper
gen diff_low_temper=low_temper-l.low_temper



sum total_phone_use distance duration speed drive_at_night highway



* just use this line to start the table
ivregress gmm diff_agg_acc l.diff_hb1_dummy diff_traffic_jam diff_weekend diff_rush_hour ///
diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
(l.diff_agg_acc = l(2).total_agg_acc) , vce(cluster user_id) nocons
outreg2 using $results/reg_nonfamiliarNM_072020_new.xls, replace keep(l.diff_hb1_dummy) 


** baseline 
foreach nm of varlist diff_hb1_dummy-diff_hb9{
	

	ivregress gmm diff_phone_use l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_phone_use = l(2).total_phone_use) , vce(cluster user_id) nocons
	outreg2 using $results/reg_nonfamiliarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_distance l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_distance = l(2).distance) , vce(cluster user_id) nocons
	outreg2 using $results/reg_nonfamiliarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_duration l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_duration = l(2).duration) , vce(cluster user_id) nocons
	outreg2 using $results/reg_nonfamiliarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_speed l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_speed = l(2).speed) , vce(cluster user_id) nocons
	outreg2 using $results/reg_nonfamiliarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_drive_at_night = l(2).drive_at_night) , vce(cluster user_id) nocons
	outreg2 using $results/reg_nonfamiliarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_highway l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_highway = l(2).highway) , vce(cluster user_id) nocons
	outreg2 using $results/reg_nonfamiliarNM_072020_new.xls, append keep(l.`nm')
	
	
}



** IV specification
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow)  , vce(cluster user_id) nocons
	outreg2 using $results/reg_nonfamiliarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow)  , vce(cluster user_id) nocons
	outreg2 using $results/reg_nonfamiliarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow)  , vce(cluster user_id) nocons
	outreg2 using $results/reg_nonfamiliarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow)  , vce(cluster user_id) nocons
	outreg2 using $results/reg_nonfamiliarNM_072020_new.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow)  , vce(cluster user_id) nocons
	outreg2 using $results/reg_nonfamiliarNM_072020_new.xls, append keep(l.`nm')
		
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow)  , vce(cluster user_id) nocons
	outreg2 using $results/reg_nonfamiliarNM_072020_new.xls, append keep(l.`nm')
		
}
















